﻿using DomainExample.DomainModel.PersonModel;

namespace DomainExample.Repository.Tests.PersonRepositoryTests
{
    public class PersonRepositoryTests : BaseRepository
    {
        protected readonly IPersonRepository Target;

        protected PersonRepositoryTests() : base(new SqlDataProvider(new ConfigConnectionStringProvider()))
        {
            var repositoryFactory = RepositoryFactory.NewRepositoryFactory();
            Target = repositoryFactory.GetPersonRepository();
        }

        protected Person AddTestPerson(string firstName, string lastName)
        {
            var testPerson = Person.NewPerson();
            testPerson.FirstName = firstName;
            testPerson.LastName = lastName;
            return Target.Persist(testPerson);
        }

        protected void CreateSchema()
        {
            var command = NewCommand(
                @"CREATE TABLE dbo.Person (
                    [Id] [int] IDENTITY(1,1) NOT NULL,
                    [FirstName] [nvarchar](50) NOT NULL,
                    [LastName] [nvarchar](50) NOT NULL,
                 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([Id] ASC) 
                WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
                ON [PRIMARY]) ON [PRIMARY]");

            ExecuteNonQuery(command);
        }

        protected void DestroySchema()
        {
            var command = NewCommand("DROP TABLE dbo.Person");
            ExecuteNonQuery(command);
        }
    }
}
